﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Topro.Web.Controllers
{
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.WarehouseUnit;
    using Topro.Util;

    /// <summary>
    /// 库位控制器
    /// </summary>
    public class WarehouseUnitController : BaseController 
    {
        #region 注入
        /// <summary>
        /// 日志接口
        /// </summary>
        private readonly LogService _logService;

        /// <summary>
        /// 仓库接口
        /// </summary>
        private readonly WarehouseService _warehouseService;

        /// <summary>
        /// 巷道接口
        /// </summary>
        private readonly TunnelService _tunnelService;
        
        /// <summary>
        /// 库位接口
        /// </summary>
        private readonly WarehouseUnitService _warehouseUnitService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logService">日志接口</param>
        /// <param name="warehouseService">库位接口</param>
        public WarehouseUnitController(LogService logService, WarehouseService warehouseService, TunnelService tunnelService, WarehouseUnitService warehouseUnitService)
        {
            _logService = logService;
            _warehouseService = warehouseService;
            _tunnelService = tunnelService;
            _warehouseUnitService = warehouseUnitService;
        }
        #endregion

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public new IActionResult Index()
        {
            return View("Index");
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>DataGrid<?></returns>
        public JsonResult WarehouseUnitQuery(WarehouseUnitQuery query)
        {
            var list = _warehouseUnitService.GetPager(query);

            return Json(list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public IActionResult WarehouseUnitAddOrEdit(string id)
        {
            var viewName = "WarehouseUnitEdit";

            var model = _warehouseUnitService.Get(id);
            //仓库列表
            var warehouses = _warehouseService.GetListByStatus((int)WarehouseStatus.启用);
            if(warehouses!=null && warehouses.Any())
            {
                ViewBag.Warehouse = warehouses.ToDictionary(item => item.Id, item => item.Name);
            }
            //状态
            ViewBag.WarehouseUnitType = (Dictionary<int, string>)EnumUtil.ToDictionary(typeof(WarehouseUnitType));

            if (!string.IsNullOrEmpty(id))
            {
                viewName = "WarehouseUnitEdit";
            }

            return View(viewName, model);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <returns></returns>
        public JsonResult UpdateStatus(string id, int status)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                response = _warehouseUnitService.UpdateStatus(id,status);
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 解锁
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public JsonResult Locked(string id)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                response = _warehouseUnitService.Locked(id);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 解锁
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public JsonResult OutLocked(string id)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                response = _warehouseUnitService.OutLocked(id);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 保存、修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult Save(WarehouseUnitSaveRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                # region 验证
                //仓库
                if(!request.WarehouseId.HasValue)
                {
                    throw new Exception("请选择仓库.");
                }
                //类型
                if (!request.Type.HasValue)
                {
                    throw new Exception("请选择类型.");
                }
                //列
                //层
                //状态
                if (!request.Status.HasValue)
                {
                    throw new Exception("选择状态.");
                }

                var status = new List<int>()
                {
                    (int)WarehouseStatus.启用,
                    (int)WarehouseStatus.禁用
                };

                if (!status.Contains(request.Status.Value))
                {
                    throw new Exception("状态参数错误.");
                }
                #endregion

                //request.Ip = Ip;
                request.CreatedBy = UserId;

                response = _warehouseUnitService.SaveWarehouseUnit(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
    }
}
